package com.hwpt.hwaccount.core.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.text.SimpleDateFormat;

/**
 * 简单的日期时间序列化工具类
 */
public class SimpleDateTimeUtil {

    public static final String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";

    /**
     * Date 转字符串
     */
    public static String dateToString(Date date) {
        return dateToString(date, DEFAULT_DATE_TIME_PATTERN);
    }

    public static String dateToString(Date date, String pattern) {
        if (date == null) return null;
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    /**
     * LocalDateTime 转字符串
     */
    public static String localDateTimeToString(LocalDateTime localDateTime) {
        return localDateTimeToString(localDateTime, DEFAULT_DATE_TIME_PATTERN);
    }

    public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {
        if (localDateTime == null) return null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    /**
     * LocalDate 转字符串
     */
    public static String localDateToString(LocalDate localDate) {
        return localDateToString(localDate, DEFAULT_DATE_PATTERN);
    }

    public static String localDateToString(LocalDate localDate, String pattern) {
        if (localDate == null) return null;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(formatter);
    }

    /**
     * 字符串转 Date
     */
    public static Date stringToDate(String dateString) {
        return stringToDate(dateString, DEFAULT_DATE_TIME_PATTERN);
    }

    public static Date stringToDate(String dateString, String pattern) {
        if (dateString == null || dateString.trim().isEmpty()) return null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            return formatter.parse(dateString);
        } catch (Exception e) {
            throw new RuntimeException("日期格式转换失败", e);
        }
    }

    /**
     * 字符串转 LocalDateTime
     */
    public static LocalDateTime stringToLocalDateTime(String dateTimeString) {
        return stringToLocalDateTime(dateTimeString, DEFAULT_DATE_TIME_PATTERN);
    }

    public static LocalDateTime stringToLocalDateTime(String dateTimeString, String pattern) {
        if (dateTimeString == null || dateTimeString.trim().isEmpty()) return null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDateTime.parse(dateTimeString, formatter);
        } catch (Exception e) {
            throw new RuntimeException("日期时间格式转换失败", e);
        }
    }

    /**
     * 字符串转 LocalDate
     */
    public static LocalDate stringToLocalDate(String dateString) {
        return stringToLocalDate(dateString, DEFAULT_DATE_PATTERN);
    }

    public static LocalDate stringToLocalDate(String dateString, String pattern) {
        if (dateString == null || dateString.trim().isEmpty()) return null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            return LocalDate.parse(dateString, formatter);
        } catch (Exception e) {
            throw new RuntimeException("日期格式转换失败", e);
        }
    }

    /**
     * 获取当前时间字符串
     */
    public static String getCurrentDateTime() {
        return localDateTimeToString(LocalDateTime.now());
    }

    /**
     * 获取当前日期字符串
     */
    public static String getCurrentDate() {
        return localDateToString(LocalDate.now());
    }
}